package com.kaishengit.service;

import java.util.List;

import com.kaishengit.dao.CommentDao;
import com.kaishengit.dao.IdearDao;
import com.kaishengit.dao.MemberDao;
import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Idear;
import com.kaishengit.entity.Member;
import com.kaishengit.util.GetTime;
import com.kaishengit.util.Pager;

public class IdearService {
	IdearDao idao = new IdearDao();
	public Pager findAll(String pid,int num) {
		Pager pager = idao.findAll(pid,num);
		List<Idear> list = pager.getResult();
		MemberDao mdao = new MemberDao();
		for(Idear i:list){
			String mid = i.getMid();
			Member m = mdao.findById(mid);
			i.setMember(m.getM_name());
		}
		return pager;
	}
	public void save(String title, String content, String p_id,String mid) {
		Idear i = new Idear();
		i.setI_id(GetTime.getTime());
		i.setI_title(title);
		i.setI_content(content);
		i.setI_createTime(GetTime.getYMDHM());
		i.setPid(p_id);
		i.setMid(mid);
		idao.insert(i);
		
	}
	public Idear findById(String iid) {
		return idao.findById(iid);
	}
	public void update(String title, String content, String i_id) {
		Idear i = new Idear();
		i.setI_content(content);
		i.setI_id(i_id);
		i.setI_title(title);
		idao.update(i);
	}
	public void del(String iid) {

		idao.del(iid);
	}
	public Member findMember(String mid) {
		MemberDao dao = new MemberDao();
		return dao.findById(mid);
	}
	public void insertComment(String iid, String content,String time, String m_id) {

		CommentDao cdao = new CommentDao();
		Comment c = new Comment();
		c.setMid(m_id);
		c.setComment_content(content);
		c.setIdeaid(iid);
		c.setComment_creatTime(time);
		c.setComment_id(GetTime.getTime());
		cdao.insert(c);
	}
	public List<Comment> listComments(String iid) {
		MemberDao mdao = new MemberDao();
		CommentDao dao = new CommentDao();
		List<Comment> list = dao.findAll(iid);
		for(Comment c:list){
			String mid = c.getMid();
			Member m = mdao.findById(mid);
			c.setMember_name(m.getM_name());
		}
		return list;
		
	}
	public void delete(String comid) {
		CommentDao dao = new CommentDao();
		dao.delete(comid);

		
	}
	

}
